<template>
  <div class="accounts-receivable-management">
    <!--    搜索部分-->
    <el-form class="search-form" :model="searchForm" inline label-position="right" label-width="7em" size="small">
      <el-row>
        <el-col :span="6">
          <el-form-item label="申请编号:" prop="payColSchAppId">
            <el-input v-model="searchForm.payColSchAppId" placeholder="请输入"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目:" prop="projectName">
            <el-input v-model="searchForm.projectName" placeholder="项目名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请标题:" prop="applicationTitle">
            <el-input v-model="searchForm.applicationTitle" placeholder="请输入"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户名称:" prop="projectCustomer">
            <el-input v-model="searchForm.projectCustomer" placeholder="客户名称"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="申请人:" prop="applicant">
            <el-input v-model="searchForm.applicant" placeholder="申请人姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请时间:" prop="createTime">
            <el-date-picker
              v-model="createTime"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="yyyy-MM-dd"
              @change="onDatePickerChange">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请状态:" prop="verifyFlag">
            <el-select v-model="searchForm.verifyFlag" placeholder="请选择">
              <el-option
                v-for="item in dict.type.process_receivable_status"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label=" " label-width="2em">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--    表格分类部分-->
    <el-tabs v-model="activeName" @tab-click="handleTabChange">
      <el-tab-pane label="全部" name="全部"/>
      <el-tab-pane label="合同回款" name="合同回款"/>
      <el-tab-pane label="保证金回款" name="保证金回款"/>
      <el-tab-pane label="其他回款" name="其他回款"/>
      <el-tab-pane label="项目决算" name="项目决算"/>
    </el-tabs>
    <!--    功能导航栏-->
    <div class="control-nav">
      <el-button type="primary" icon="el-icon-plus" @click="handleAddPlan">添加回款计划</el-button>
      <el-button type="primary">项目决算</el-button>
      <el-button type="primary" plain>导出</el-button>
    </div>
    <!--    表格-->
    <p-table class="table" ref="pTable" :data="tableData" pagination :get-list="getList">
      <el-table-column align="center" min-width="192" label="申请编号" prop="payColSchAppId"
                       fixed="left"></el-table-column>
      <el-table-column align="center" min-width="96" label="回款类型" prop="collectionType">
        <template v-slot="{row}">
          <div>{{ tabMap[row.collectionType] }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="144" label="申请标题" prop="applicationTitle"></el-table-column>
      <el-table-column align="center" min-width="112" label="应收金额(元)" prop="amountReceivable"></el-table-column>
      <el-table-column align="center" min-width="144" label="项目名称" prop="projectName"></el-table-column>
      <el-table-column align="center" min-width="144" label="客户名称" prop="projectCustomer"></el-table-column>
      <el-table-column align="center" min-width="96" label="客户经理" prop="customerManager"></el-table-column>
      <el-table-column align="center" min-width="96" label="申请人" prop="applicant"></el-table-column>
      <el-table-column align="center" min-width="154" label="申请时间" prop="createTime"></el-table-column>
      <el-table-column align="center" min-width="96" label="审批状态" prop="verifyFlag" fixed="right">
        <template v-slot="{row}">
          <div class="verify-flag" :style="`--fontColor:${statusColorMap[statusColorMap[row.verifyFlag]]}`">
            {{ statusColorMap[row.verifyFlag] }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="144" label="操作" fixed="right">
        <template v-slot="{row}">
          <el-button type="text" @click="handleGoDetail(row.payColSchAppId)">详情</el-button>
          <template v-if="[1,4].includes(row.verifyFlag)">
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="handleEdit(row.payColSchAppId)">修改</el-button>
          </template>
          <template v-if="[1,2].includes(row.verifyFlag)">
            <el-divider direction="vertical"></el-divider>
            <el-button type="text">撤销</el-button>
          </template>
        </template>
      </el-table-column>
    </p-table>
  </div>
</template>

<script>
import pTable from '@/components/pTable/index.vue'
import {apiGetCollectionList} from "@/api/process-control-platform/sale/collectionPlan";

// 搜索表单原始对象
const originSearchForm = {
  payColSchAppId: '',     // 申请编号
  projectName: '',        // 项目
  applicationTitle: '',   // 申请标题
  projectCustomer: '',    // 客户名称
  applicant: '',          // 申请人
  startTime: '',          // 开始时间
  endTime: '',            // 结束时间
  verifyFlag: ''          // 申请状态
}
// tab映射字典
const tabMap = {
  ['全部']: 0,
  ['合同回款']: 1,
  ['保证金回款']: 2,
  ['其他回款']: 3,
  ['项目决算']: 4,
  1: '合同回款',
  2: '保证金回款',
  3: '其他回款',
  4: '项目决算',
}
// 审批状态字体颜色映射关系
const statusColorMap = {
  1: '待审批',
  2: '审批中',
  3: '已通过',
  4: '未通过',
  5: '已撤销',
  ['待审批']: '#FF7C3D',
  ['审批中']: '#F59A23',
  ['已通过']: '#1890FF',
  ['未通过']: '#FF2323',
  ['已撤销']: '#43D59E',
}

export default {
  name: "accountsReceivableManagement",
  dicts: ['process_receivable_status'],
  components: {pTable},
  data() {
    return {
      searchForm: Object.assign({}, originSearchForm),
      createTime: [],
      activeName: '全部',
      tableData: [],
      statusColorMap,
      tabMap,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.pTable.resetPage()
    })
  },
  methods: {
    getList({current, size}) {
      const collectionType = tabMap[this.activeName]
      const req = {...this.searchForm, pageNum: current, pageSize: size, collectionType}
      return apiGetCollectionList(req).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.list
        }
      })
    },

    resetSearchForm() {
      this.$set(this, 'searchForm', Object.assign({}, originSearchForm))
      this.createTime = []
      this.$refs.pTable.resetPage()
    },

    handleReset() {
      this.resetSearchForm()
    },

    // 时间选择器值变化的回调
    onDatePickerChange(value) {
      if (value instanceof Array) {
        this.searchForm.startTime = value[0]
        this.searchForm.endTime = value[1]
      } else {
        this.searchForm.startTime = null
        this.searchForm.endTime = null
      }
    },

    handleSearch() {
      this.$refs.pTable.resetPage()
    },

    // 表格分类tab时的回调
    handleTabChange() {
      this.$refs.pTable.resetPage()
    },

    // 跳转添加回款计划页面
    handleAddPlan() {
      this.$router.push({
        path: '/sale/accountsReceivableManagement/addPlan'
      })
    },

    // 跳转到回款计划详情页面
    handleGoDetail(id) {
      this.$router.push({
        path: '/sale/accountsReceivableManagement/planDetails',
        query: {id}
      })
    },

    // 跳转到修改页
    handleEdit(id) {
      this.$router.push({
        path: '/sale/accountsReceivableManagement/updatePlan',
        query: {
          id
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.accounts-receivable-management {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 84px);
  min-height: 600px;
  padding: 20px;
  overflow: auto;

  .search-form {
    ::v-deep {
      .el-form-item {
        display: flex;
        //min-width: 300px;

        .el-form-item__label {
          flex-shrink: 0;
        }

        .el-form-item__content {
          flex: 1;
        }

        .el-date-editor.el-input__inner {
          width: 100%;
        }

        .el-select {
          width: 100%;
        }
      }
    }
  }

  ::v-deep {
    .el-tabs__nav {
      .el-tabs__item {
        padding: 0 20px;
      }
    }
  }

  .control-nav {
    margin-top: 10px;
    margin-bottom: 20px;
  }

  .table {
    flex: 1;
    height: 0;

    .verify-flag {
      color: var(--fontColor);
    }
  }
}
</style>
